<template>
	<view class="page">
		<u-navbar title="我的减重" border @leftClick="$utils.back()" placeholder />
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view v-for="item in data" :key="item.id" class="contentItem" @click="jump(item)">
						<view class="imageBox" @click.stop="()=>{}">
							<view @click='openSelect(item)'>
								<u-icon name="edit-pen-fill" color='#fff'></u-icon>
							</view>
							<image mode="aspectFill" :src="item.thumb" />
						</view>
						<view class="contentInfo">
							<view>
								{{item.title}}
							</view>
							<view>
								<view class="icon">
									{{item.is_public?'公开':'个人'}}
								</view>
								<view class="icon">
									{{item.is_hide?'隐藏':'显示'}}
								</view>
							</view>
							<view>
								开始时间:{{item.start_at}}
							</view>
						</view>
						<view @click.stop="()=>{}">
							<u-icon name="more-dot-fill" size="50rpx" @click='openRecordSelect(item)'></u-icon>
						</view>
					</view>
				</template>
				<template v-else>
					<view v-for="item in 8" :key="item" class="contentItem">
						<view class="Skeleton" style="width: 160rpx;height: 160rpx;" />
						<view class="contentInfo">
							<view style="height: 30rpx;width: 100%;" class="Skeleton" v-for="item in 3" :key="item" />
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<view class="add " @click="jumpAdd" v-if="!loading">新增活动
		</view>
		<root-portal>
			<u-modal :show="modalShow" :showCancelButton='true' @close='cancel' @cancel='cancel' @confirm='update'>
				<view style="width: 100%;">
					<view class="popupTitle">
						<u-icon name='close' color='#fff' />
						<view>
							编辑挑战
						</view>
						<u-icon name='close' @click='cancel' />
					</view>
					<view style="padding: 0 40rpx;">
						<u--form labelWidth='100' labelPosition="top" :model="form" :rules="rules" ref="uForm">
							<u-form-item label="体重:" prop="weight" borderBottom>
								<u--input type='number' v-model="form.weight" placeholder="请输入体重,注意单位为Kg" />
							</u-form-item>
							<u-form-item label="想说的话:" prop="motto" borderBottom>
								<u--input v-model="form.motto" placeholder="请输入想说的话" />
							</u-form-item>
						</u--form>
					</view>
				</view>
			</u-modal>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" showCancelButton @confirm='del()' @cancel='delShow=false'>
				<view style="padding: 12px 25px 25px 25px;font-size: 30rpx;">
					是否确认删除
				</view>
			</u-modal>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				modalShow: false,
				form: {
					weight: null,
					motto: null
				},
				rules: {
					'weight': [{
						required: true,
						message: '请输入体重',
						trigger: ['blur', 'change']
					}, {
						validator: (rule, value, callback) => {
							return value > 0
						},
						message: '体重必须大于0',
						trigger: ['blur', 'change']
					}],
					'motto': {
						type: 'string',
						required: true,
						message: '请输入想说的话',
						trigger: ['blur', 'change']
					},
				},
				type: 'conquer',
				data: [],
				loading: true,
				selectItem: {},
				selectShow: false,
				delShow: false,
				disabled: false
			}
		},
		computed: {
			columns() {
				if (this.type == 'record') {
					return [{
						name: '编辑'
					}, {
						name: '退出'
					}, ]
				}
				return [{
						name: '编辑'
					},
					{
						name: '删除'
					}, {
						name: '退出'
					},
				]
			}
		},
		methods: {
			cancel() {
				if (this.disabled) return
				this.$refs.uForm.resetFields()
				this.form = {
					weight: null,
					motto: null
				}
				this.modalShow = false
			},
			openRecordSelect(item) {
				this.type = 'record'
				this.selectShow = true
				this.selectItem = item
			},
			update() {
				if (this.disabled) return
				this.$refs.uForm.validate().then(res => {
					this.disabled = true
					this.$api.Conquer.Record.Update(this.selectItem.id, this.form).then(res => {
						uni.$u.toast('加入成功')
						this.disabled = false
						this.modalShow = false
						this.$refs.paging.reload()
						this.cancel()
					}).catch(err => {
						this.disabled = false
						uni.$u.toast(err.data.message)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			del() {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api.Conquer.Del(this.selectItem.id).then(err => {
					this.data = this.data.filter(item => item.id != this.selectItem.id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (this.type == 'conquer') {
					console.log(e.name)
					if (e.name == '编辑') {
						this.$utils.navigateTo({
							url: '/Conquer/update?id=' + this.selectItem.id
						})
					} else if (e.name == '删除') {

						this.delShow = true
					} else if (e.name == '退出') {

						this.selectShow = true
					}
				} else {
					if (e.name == '编辑') {
						this.modalShow = true
						this.form = {
							weight: this.selectItem.weight,
							motto: this.selectItem.motto
						}
						setTimeout(() => {
							this.$refs.uForm.setRules(this.rules)
						}, 500)
					} else if (e.name == '退出') {

						this.selectShow = true
					}
				}


			},
			openSelect(item) {
				this.type = 'conquer'
				this.selectShow = true
				this.selectItem = item
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/Conquer/add'
				})
			},
			jump(item) {
				if (!item.has_started) {
					this.$utils.navigateTo({
						url: "/Conquer/detailNoStar?id=" + item.id
					})
				} else if (item.is_public) {
					this.$utils.navigateTo({
						url: "/Conquer/Detail?id=" + item.id
					})
				} else {
					this.$utils.navigateTo({
						url: "/User/detail?conquer_id=" + item.id + '&id=' + item.user_id
					})
				}
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.My.Conquer(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.data)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		border-radius: 100rpx;
		background-color: #ddd;
		text-align: center;
		padding: 20rpx;
	}

	.page {
    box-sizing: border-box;
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.contentItem {
		padding: 20rpx 0;
		display: flex;
		border-bottom: 1rpx solid #f5f5f5;

		image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 8rpx;
		}

		.imageBox {
			position: relative;

			>view {
				background: rgba(0, 0, 0, 0.3);
				border-radius: 100%;
				padding: 10rpx;
				position: absolute;
				top: 10rpx;
				left: 10rpx;
			}
		}

		.contentInfo {
			flex: 1;
			margin: 0 20rpx;
			display: flex;
			flex-direction: column;
			height: 160rpx;
			justify-content: space-between;
		}

		.icon {
			background-color: #ddd;
			border-radius: 100rpx;
			padding: 8rpx 20rpx;
			display: inline-block;
			margin-right: 10rpx;
		}
	}
</style>